package config

import (
	"go.uber.org/zap"
	"gopkg.in/yaml.v3"
	"gorm.io/driver/mysql"
	"os"
)

type AlertWebhookConfig struct {
	HttpAddr                        string        `yaml:"http_addr"`
	LogLevel                        string        `yaml:"log_level"`
	LogFilePath                     string        `yaml:"log_file_path"`
	AlertReceiveQueueSize           int           `yaml:"alert_receive_queue_size"`
	CommonMapRenewIntervalSeconds   int           `yaml:"common_map_renew_interval_seconds"`
	MysqlC                          *mysql.Config `yaml:"mysql"` // 我们不需要自己定义 gorm 连接mysql yaml 配置了 ，grom库中有 ，导入即可
	HttpRequestGlobalTimeoutSeconds int           `yaml:"http_request_global_timeout_seconds"`
	AlertManagerApi                 string        `yaml:"alert_manager_api"`
	FrontDomain                     string        `yaml:"front_domain"`
	BackendDomain                   string        `yaml:"backend_domain"`
	HostName                        string        `yaml:"-"`
	LocalIp                         string        `yaml:"-"`
	ImFeishuC                       *ImFeishu     `yaml:"im_feishu"`
	Logger                          *zap.Logger   `yaml:"-"` // 不是配置文件提供的，而是 共用配置文件这个结构体，在生成完logger后设置的
}

type ImFeishu struct {
	QunLiaoMessageApi     string `yaml:"qun_liao_message_api"`
	SiLiaoMessageApi      string `yaml:"si_liao_message_api"`
	TenantAccessTokenApi  string `yaml:"tenant_access_token_api"`
	SiLiaoRobotAppId      string `yaml:"si_liao_robot_appid"`
	SiLiaoRobotAppSecret  string `yaml:"si_liao_robot_appsecret"`
	RequestTimeoutSeconds int    `yaml:"request_timeout_seconds"`
}

// 根据io read读取配置文件后的字符串解析yaml
func LoadAlertWebhook(filename string) (*AlertWebhookConfig, error) {
	cfg := &AlertWebhookConfig{}

	content, err := os.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	err = yaml.Unmarshal(content, cfg)
	if err != nil {
		return nil, err
	}

	// 加载配置文件的时候就塞进去

	return cfg, err

}
